
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
using FlowLib.Containers;
using ClientExample.Comparers;
using ClientExample.Converters;
using ClientExample.Filters;
using ClientExample.Containers;
using System.Windows.Forms;
using System;

namespace ClientExample.Controls
{
    public class ListViewSearchResult : ListViewExtended<SearchResultGroup>
    {
        public ListViewSearchResult()
            : base()
        {
            AllItems.Sort(new SearchResultGroupComparer(SearchResultGroupComparer.Columns.ID));
            Items.Sort(new SearchResultGroupComparer(SearchResultGroupComparer.Columns.DisplayName));
            Converter = new SearchResultGroupConverter();
            Filter = new SearchResultGroupFilter();
            this.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(ListViewSearchResult_ColumnClick);

            ColumnHeader headerName = new ColumnHeader();
            headerName.Text = "Name";
            headerName.Tag = SearchResultGroupComparer.Columns.DisplayName.ToString();
            headerName.Width = 450;
            Columns.Add(headerName);

            ColumnHeader headerSource = new ColumnHeader();
            headerSource.Text = "Source(s)";
            headerSource.Tag = SearchResultGroupComparer.Columns.Sources.ToString();
            headerSource.Width = 60;
            Columns.Add(headerSource);

            ColumnHeader headerType = new ColumnHeader();
            headerType.Text = "Type";
            headerType.Tag = SearchResultGroupComparer.Columns.Type.ToString();
            headerType.Width = 60;
            Columns.Add(headerType);

            ColumnHeader headerSize = new ColumnHeader();
            headerSize.Text = "Size";
            headerSize.Tag = SearchResultGroupComparer.Columns.Size.ToString();
            headerSize.Width = 80;
            Columns.Add(headerSize);
        }

        void ListViewSearchResult_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            SearchResultGroupComparer comp = null;
            SearchResultGroupComparer.Columns column = (SearchResultGroupComparer.Columns)Enum.Parse(typeof(SearchResultGroupComparer.Columns), this.Columns[e.Column].Tag.ToString());
            comp = new SearchResultGroupComparer(column);
            if (((SearchResultGroupComparer)Items.Comparer).SortOnColumn == column)
            {
                comp.Reverse = !((SearchResultGroupComparer)Items.Comparer).Reverse;
            }
            Items.Sort(comp);
            this.Invalidate(true);
        }
    }
}
